package CS4310.csueb.main;

import java.awt.Color;
import java.awt.event.*;

import javax.swing.*;

import CS4310.csueb.characters.Hero;
import CS4310.csueb.story.QuestManager;
import CS4310.csueb.visual.DrawHeroSprite;
import CS4310.csueb.visual.HeroSprite;
import CS4310.csueb.visual.MapPanel;
import CS4310.csueb.visual.SpriteManager;


//////////////////////////// class GameManager///////////////////////////
//handles map scrolling 
//integrates game components: maps, sprites, quest
//and generally monitors/guides the game itself
public class GameManager extends JPanel implements ActionListener, KeyListener {
	
	private static final int PORTWIDTH = 8;  // Absolute Max WIDTH for this Map Port.
	private static final int PORTHEIGHT = 5;  // Absolute Max HEIGHT for this Map Port.
	  
	private int xOffset = 0, yOffset = 0;
	private static final long serialVersionUID = 991034413360996410L;
	private MapPanel startMap;
	private JLayeredPane layeredPane;
	private SpriteManager zoneSprites;
	private QuestManager questsLog;
	private HeroSprite ourHero;
	private DrawHeroSprite sprite;
	
	private int x, y;
	
	private String f = "CS4310/csueb/visual/img/mochimanfront.gif";
	
	public GameManager(){
		setLayout(null);
		setBackground(Color.BLACK);
		
		//ourHero = new HeroSprite(f,"s");
		
		//centering the Sprite on the MAP
		//x = 400 - ourHero.getWidth();
		//y = 600/2 - ourHero.getHeight();
		
		//NOTE--will have to eventually use/develop something that is integrated with the map
		//sprite = new DrawHeroSprite(f,x,y);
		//add(sprite);
		
		// to Test drawMap
		//startMap = new MapPanel("CS4310/csueb/visual/img/thestart.jpg");
		startMap  = new MapPanel();
		startMap.setMapName("Westin");
		//startMap.oldMTCBuildLoader();  // Old Style Build Loader to load row,col style
		startMap.MTCBuildLoader("Westin");
		//startMap.scrollMap(xOffset, yOffset);
		//startMap.repaint();
		startMap.addKeyListener(this);
		//startMap.setFocusable(true);
		//add(startMap, 1, 1);
		//startMap.saveGameMap();
		//add(sprite, 2, 0);
		//add(startMap,1);
		//add(sprite,0);
		
        layeredPane = new JLayeredPane();
		layeredPane.setLayout(null);
		// layeredPane.setPreferredSize(new Dimension(800,80));
		layeredPane.setBounds(0, 0, 800, 500);
		layeredPane.setBackground(Color.WHITE);
		
		layeredPane.add(startMap,  new Integer(1), 0);

	    ourHero = new HeroSprite("CS4310/csueb/visual/img/Male_Face.gif", 300,200);
	    ourHero.setBounds(300, 200, 100, 100);
	    layeredPane.add(ourHero, new Integer(2), 0);

	    this.add(layeredPane);
	    
		
		questsLog = new QuestManager();
		//ourHero = new HeroSprite("CS4310/csueb/visual/img/Male_Face.gif", "Bill", 100, 10, 5, 10);
		//zoneSprites = new SpriteManager(ourHero);
		//add(zoneSprites, 0);
		
	}
	
	
	//Periodically repaint MapPanel in relation to HeroSprite's x and y locations?
	//********* key Typed **********//  
	  public void keyTyped(KeyEvent e) {
		  int keyCode = e.getKeyCode();
		  if (keyCode == KeyEvent.VK_ESCAPE) {
	             System.exit(0);
	        }
		  e.consume();
	  }
	  
	//********* key Pressed **********//  
	  public void keyPressed(KeyEvent e) { 
		  
		  int keyCode = e.getKeyCode();
		  
		  xOffset = ourHero.getx();
		  yOffset = ourHero.gety();
		  
		  if (keyCode == KeyEvent.VK_LEFT && xOffset >= 100) {
			  xOffset -= 100;
		  }
		  else if (keyCode == KeyEvent.VK_RIGHT && 
				   xOffset <= (PORTWIDTH*100)-200) {
			  xOffset += 100;
		  }
		  else if (keyCode == KeyEvent.VK_UP && yOffset >= 100) {
			  yOffset -= 100;
		  }
		  else if (keyCode == KeyEvent.VK_DOWN &&
				   yOffset <= (PORTHEIGHT*100)-200) {
			  yOffset += 100;
		  }
		  ourHero.setXandY(xOffset, yOffset);
		  ourHero.setBounds(xOffset, yOffset, 100, 100);
}
	   
	//********* key Released **********//  
	  public void keyReleased(KeyEvent e) {
		  int keyCode = e.getKeyCode();
		  if (keyCode == KeyEvent.VK_LEFT) {
			  //move map to right
		  }
		  if (keyCode == KeyEvent.VK_RIGHT) {
			  //move map to left
		  }
		  if (keyCode == KeyEvent.VK_UP) {
			  //move map to Down
		  }
		  if (keyCode == KeyEvent.VK_DOWN) {
			  //move map to Up
		  }
		  
		  //so key isn't processed for others
		  e.consume();
	  }

	//********* Action Performed **********//  
	  public void actionPerformed(ActionEvent e) {
		  //super.actionPerformed(e);
	  }
}
